//
//  RatingViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/8.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit
import RxSwift

class RatingViewController: UIViewController {

    let gradingView = GradingView()
    
    var orderVM: OrderViewModel?
    
    var orderId: Int?
    
    let disposeBag = DisposeBag()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    let textView: UITextView = {
        let textView = UITextView()
        textView.text = LanguageHelper.getString(key: "WRITE_SOMETHING")
        textView.textColor = UIColor.lightGray
        textView.font = AppFont(size: 20)
        textView.returnKeyType = .default
        return textView
    }()
    
    var textViewHasEdited: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = LanguageHelper.getString(key: "WRITE_COMMENT")

        setBackBarItem()
        
        let rightBt = UIButton()
        rightBt.contentHorizontalAlignment = .right
        rightBt.setTitle(LanguageHelper.getString(key: "SUBMIT"), for: .normal)
        rightBt.setTitleColor(doneItemButtonColor, for: .normal)
        rightBt.addTarget(self, action: #selector(submit(_:)), for: .touchUpInside)
        rightBt.sizeToFit()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightBt)
        
        gradingView.frame = CGRect(x: 25, y: 25, width: self.view.bounds.width - 50, height: (self.view.bounds.width - 50) / 6)
        
        saperator.frame = CGRect(x: 25, y: gradingView.frame.origin.y + gradingView.frame.height + 15, width: self.view.bounds.width - 50, height: 0.5)
        self.view.addSubview(saperator)
        self.view.addSubview(gradingView)
        
        addTextView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func submit(_ sender: UIBarButtonItem) {
        if let vm = orderVM, let id = orderId {
            let review = textViewHasEdited ? textView.text! : ""
            vm.postOrderReviews(id: id, rating: gradingView.grade, review: review).subscribe(onNext: { [unowned self] json in
                    print("json: \(json)")
                    self.navigationController?.popToRootViewController(animated: true)
                }, onError: { [unowned self] error in
                    self.showPromptView(message: LanguageHelper.getString(key: "REVIEW_FAIL"), confirmText: LanguageHelper.getString(key: "OK"))
            })
            .addDisposableTo(disposeBag)
        }
    }
    
    func addTextView() {
        self.textView.frame = CGRect(x: 25, y: gradingView.frame.origin.y + gradingView.frame.height + 40, width: self.view.frame.width - 50, height: 100)
        self.textView.delegate = self
        self.view.addSubview(textView)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let point = touch.location(in: self.view)
            if !textView.frame.contains(point) {
                textView.resignFirstResponder()
            }
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension RatingViewController: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        if !textViewHasEdited {
            textView.text = ""
            textView.textColor = UIColor.black
            textViewHasEdited = true
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text == "" {
            textView.text = LanguageHelper.getString(key: "WRITE_SOMETHING")
            textView.textColor = UIColor.gray
            textViewHasEdited = false
        }
    }
}
